<form action="#" class="am-form">
    <fieldset>
        <legend>群名称</legend>
        <div class="form_content">
            <div class="input">
                <input type="text" id="name" placeholder="输入群的名称" class="am-form-field" required/>
            </div>
            <div class="button">
                <button type="button" class="am-btn am-btn-primary btn-name">保存</button>
            </div>
            <div class="clear"></div>
        </div>

        <legend>群头像</legend>
        <div class="form_content">
            <img src="" class="icon"/>
            <input type="file" class="icon" data-url="/tephra/ctrl-http/upload" multiple>
        </div>

        <div class="niuniu" style="display:none;">
            <legend>牛牛设置</legend>
            <div class="form_content">
                <div class="input">
                    <input type="text" id="niuniu-redpacketSubject" placeholder="开奖红包标题" class="am-form-field" required/>
                </div>
                <div class="button">开奖红包的显示标题</div>
                <div class="clear"></div>

                <div class="input">
                    <input type="number" id="niuniu-maxRate" placeholder="冻结金额倍数" class="am-form-field" required/>
                </div>
                <div class="button">冻结金额倍数，一般设置为最大赔率</div>
                <div class="clear"></div>

                <div class="input">
                    <input type="text" id="niuniu-start" placeholder="游戏开始的说明" class="am-form-field" required/>
                </div>
                <div class="button">游戏开始时，将发送本内容到群消息列表中</div>
                <div class="clear"></div>

                <div class="input">
                    <input type="text" id="niuniu-end" placeholder="游戏封盘的说明" class="am-form-field" required/>
                </div>
                <div class="button">游戏封盘时，将发送本内容到群消息列表中</div>
                <div class="clear"></div>

                <div class="input">
                    <input type="text" id="niuniu-open" placeholder="游戏开奖的说明" class="am-form-field" required/>
                </div>
                <div class="button">游戏开奖时，将发送本内容到群消息列表中</div>
                <div class="clear"></div>

                <div class="input">
                    <input type="number" id="niuniu-endTime" placeholder="封盘时间" class="am-form-field" required/>
                </div>
                <div class="button">封盘时间，单位：秒</div>
                <div class="clear"></div>

                <div class="input">
                    <input type="number" id="niuniu-openTime" placeholder="开奖时间" class="am-form-field" required/>
                </div>
                <div class="button">开奖时间，单位：秒</div>
                <div class="clear"></div>
                
                <div class="input">
                    <input type="number" id="niuniu-auto" placeholder="自动进入下一局" class="am-form-field" required/>
                </div>
                <div class="button">自动进入下一局的时间，单位：秒。如果填写0表示手工开启</div>
                <div class="clear"></div>
                
                <div class="input">
                    <input type="number" id="niuniu-drawTime" placeholder="领取红包超时时间" class="am-form-field" required/>
                </div>
                <div class="button">用户超过多久没有领取红包，系统将自动为其领取，单位：秒。</div>
                <div class="clear"></div>

                <div class="button">
                    <button type="button" class="am-btn am-btn-primary">保存</button>
                </div>
            </div>
        </div>
    </fieldset>
</form>
<style type="text/css">
    image.icon {
        border: 1px solid #ccc;
        width: 120px;
        height: 120px;
    }
    .niuniu .form_content>div {
        margin-bottom: 5px;
    }
</style>

<script>
    // 等待一切就绪
    waitOk(function() {
        if (!GroupUtil.group) return;
        $("#name").val(GroupUtil.group.name);
        $("img.icon").attr("src", GroupUtil.group.portrait);

        if (GroupUtil.group.lottery == 'niuniu') {
            $(".niuniu").show();
            CMD.lottery_niuniu_find(GroupUtil.group.id, function(data) {
                $("#niuniu-maxRate").val(data.maxRate || 0);
                $("#niuniu-redpacketSubject").val(data.redpacketSubject || '祝君中奖');
                $("#niuniu-start").val(data.start || '');
                $("#niuniu-end").val(data.end || '');
                $("#niuniu-open").val(data.open || '');
                $("#niuniu-endTime").val(data.endTime || 0);
                $("#niuniu-openTime").val(data.openTime || 0);
                $("#niuniu-auto").val(data.auto || 0);
                $("#niuniu-drawTime").val(data.drawTime || 0);
            });
        }
        else {
            $(".niuniu").hide();
        }
    });

    $("input.icon").change(function() {
        var data = new FormData();
        var file = $("input.icon")[0].files[0];
        if (file.size > 200 * 1024) {
            alert2("头像大小必须小于200K！");
            return;
        }
        data.append("eddy.message.image", $("input.icon")[0].files[0]);
        
        startLoading();
        CMD.upload(data, null, function(r) {
            stopLoading();
            $("img.icon").attr("src", r);
            
            CMD.set_group_portrait(GroupUtil.group.id, r);
        });
    });

    $(".btn-name").click(function() {
        var v = $("#name").val().trim();
        if (v == "") {
            alert2("请输入群的名称！");
            return;
        }
        startLoading();
        CMD.set_group_name(GroupUtil.group.id, v, function() {
            stopLoading();
            alert2("操作成功，重新登录菠菜后起效！");
        });
    });

    $(".niuniu button").click(function() {
        var data = {};
        data.maxRate = $("#niuniu-maxRate").val() || '0';
        data.redpacketSubject = $("#niuniu-redpacketSubject").val() || '祝君中奖';
        data.start = $("#niuniu-start").val().trim() || '';
        data.end = $("#niuniu-end").val().trim() || '';
        data.open = $("#niuniu-open").val().trim() || '';
        data.endTime = $("#niuniu-endTime").val().trim() || '0';
        data.openTime = $("#niuniu-openTime").val().trim() || '0';
        data.auto = $("#niuniu-auto").val().trim() || '0';
        data.drawTime = $("#niuniu-drawTime").val().trim() || '0';

        startLoading();
        CMD.lottery_niuniu_save(GroupUtil.group.id, data, function() {
            stopLoading();
            alert2("保存成功");
        })
    })
</script>
   